package com.dimong.www.loan.ui.act;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dimong.loan.sdk.ui.act.BaseFragmentActivity;
import com.dimong.loan.sdk.ui.frag.BaseFragment;
import com.dimong.www.loan.R;
import com.dimong.www.loan.ui.frag.main.MainFourFrag;
import com.dimong.www.loan.ui.frag.main.MainOne2Frag;
import com.dimong.www.loan.ui.frag.main.MainOneFrag;
import com.dimong.www.loan.ui.frag.main.MainThreeFrag;
import com.dimong.www.loan.ui.frag.main.MainTwoFrag;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends BaseFragmentActivity implements OnClickListener {
    private static final int MAIN_ONE = 0;
    private static final int MAIN_TWO = 1;
    private static final int MAIN_THREE = 2;

    private List<BaseFragment> listViews;
    private View ll_main_one;
    private View ll_main_two;
    private View ll_main_three;

    private ImageView iv_main_one;
    private ImageView iv_main_two;
    private ImageView iv_main_three;

    private TextView tv_main_one;
    private TextView tv_main_two;
    private TextView tv_main_three;

    private MainOne2Frag mMainOneFrag;
    private MainThreeFrag mMainTwoFrag;
    private MainFourFrag mMainThreeFrag;
    private int index;
    private int currentTabIndex;
    private long exitTime;

    @Override
    public void handleActivityMessage(Message msg) {
        super.handleActivityMessage(msg);

    }

    @Override
    public void initIntentParam(Intent intent) {

    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public int getContentView() {
        return R.layout.act_main2;
    }

    @Override
    public void initView() {
        ll_main_one = this.findViewById(R.id.ll_main_one);
        ll_main_two = this.findViewById(R.id.ll_main_two);
        ll_main_three = this.findViewById(R.id.ll_main_three);

        iv_main_one = findViewById(R.id.iv_main_one);
        iv_main_two = findViewById(R.id.iv_main_two);
        iv_main_three = findViewById(R.id.iv_main_three);

        tv_main_one = findViewById(R.id.tv_main_one);
        tv_main_two = findViewById(R.id.tv_main_two);
        tv_main_three = findViewById(R.id.tv_main_three);
    }

    @Override
    public void setViewStatus() {
        ll_main_one.setOnClickListener(this);
        ll_main_two.setOnClickListener(this);
        ll_main_three.setOnClickListener(this);

        listViews = new ArrayList<>();
        if (mMainOneFrag == null) {
            mMainOneFrag = new MainOne2Frag();
        }

        if (mMainTwoFrag == null) {
            mMainTwoFrag = new MainThreeFrag();
        }
        if (mMainThreeFrag == null) {
            mMainThreeFrag = new MainFourFrag();
        }
        listViews.add(mMainOneFrag);
        listViews.add(mMainTwoFrag);
        listViews.add(mMainThreeFrag);

        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        trx.add(R.id.fragment_container, listViews.get(0)).commitAllowingStateLoss();
        changeImageButtonSrc(currentTabIndex);

        ll_main_one.setVisibility(View.VISIBLE);
        ll_main_two.setVisibility(View.VISIBLE);
        ll_main_three.setVisibility(View.VISIBLE);
    }

    @Override
    public void onActResume() {
    }

    @Override
    public void onActPause() {
    }

    @Override
    public void onActDestroy() {
    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    private void changeImageButtonSrc(int index) {
        switch (index) {
            case MAIN_ONE:
                iv_main_one.setSelected(true);
                iv_main_two.setSelected(false);
                iv_main_three.setSelected(false);

                tv_main_one.setSelected(true);
                tv_main_two.setSelected(false);
                tv_main_three.setSelected(false);
                break;
            case MAIN_TWO:
                iv_main_one.setSelected(false);
                iv_main_two.setSelected(true);
                iv_main_three.setSelected(false);

                tv_main_one.setSelected(false);
                tv_main_two.setSelected(true);
                tv_main_three.setSelected(false);
                break;
            case MAIN_THREE:
                iv_main_one.setSelected(false);
                iv_main_two.setSelected(false);
                iv_main_three.setSelected(true);

                tv_main_one.setSelected(false);
                tv_main_two.setSelected(false);
                tv_main_three.setSelected(true);
                break;
            default:
                break;
        }
    }


    @Override
    public void onClick(View arg0) {
        int id = arg0.getId();
        switch (id) {
            case R.id.ll_main_one:
                index = 0;
                break;
            case R.id.ll_main_two:
                index = 1;
                break;
            case R.id.ll_main_three:
                index = 2;
                break;
            default:
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(listViews.get(currentTabIndex));
            if (!listViews.get(index).isAdded()) {
                trx.add(R.id.fragment_container, listViews.get(index));
            }
            trx.show(listViews.get(index)).commitAllowingStateLoss();
            currentTabIndex = index;
            changeImageButtonSrc(currentTabIndex);
        }
    }

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - exitTime == 0 || currentTime - exitTime > 1500) {
            exitTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        } else {
            super.onBackPressed();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }
}
